package yunquan.wang.base.appcode;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import yunquan.wang.base.spring.SpringContextHolder;
import yunquan.wang.core.service.ServiceException;
import yunquan.wang.modules.base.entity.CodeValue;
import yunquan.wang.modules.base.service.AppCodeService;

public class AppcodeHolder {
	//Cache
	private static Map<String, Set<CodeValue>> codeCache = new HashMap<String, Set<CodeValue>>();
	private static Boolean init=false;
	private static AppCodeService appCodeService;
	/**
	 * 根据代码类型和代码，得到值
	 * @param type
	 * @param code
	 * @return
	 */
	public static Object getCodeValue(String type, String code) {
		Set<CodeValue> typeCode = getAllCodeValue(type);
		for (CodeValue single : typeCode) {
			if (single.getCode().equalsIgnoreCase(code))
			{
				return single.getValue();
			}
		}
		if(StringUtils.isBlank(code)) return "";
		
		return "";
	//	throw new ServiceException("没有找到此码表的值，请检查配置");
	}
	/**
	 * 根据代码类型获取到代码的列表
	 * @param type
	 * @return
	 */
	public static Set<CodeValue> getAllCodeValue(String type) {
		init();
		Set<CodeValue> typeCode = codeCache.get(type);
		if (typeCode == null) {
			typeCode=	appCodeService.getAllCodeValue(type);
			codeCache.put(type, typeCode);
		}
		return typeCode;
	}
	private static void init() {
	 
		if(!init){
			appCodeService=SpringContextHolder.getBean(AppCodeService.class);
		}
		
	}
}
